﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmartRoute.Data;
using System.Text.RegularExpressions;
using System.Threading;
using System.Runtime.Serialization;
using System.Xml.Serialization;
using SmartRoute.BingMaps;

namespace SmartRoute.Analytics
{
    public class AlgorithmTesting
    {

        //THIS CLASS IS FOR TESTING PURPOSES, IN GENERAL YOU CAN DISREGARD.
        public static void main(String[] args)
        {

        }
        

        bool foundBetter;

        public bool FoundBetter

        {
            get { return foundBetter; }
            set { foundBetter = value; }
        }

        Imagery display;

        public Imagery Display
        {
            get { return display; }
            set { display = value; }
        }

        public AlgorithmTesting()
        {
            foundBetter = false;

        }

        public void testScalability()
        {

        }

        public void testCorrectness()
        {
            //initialize modules used
            RoutingServices routeServ = new RoutingServices("AhqjOSdmQlNyiAfMcr8HicreBdxgTnuVaiMGVFnP9x3l_12WQgLyV1pRrfIpECX5");

            //bring stops in from XML
            var serializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            System.IO.StreamReader temp = new System.IO.StreamReader(@"C:\Documents and Settings\cs480f10_19\Desktop\smartroute\dev\data\Mt. Stuart Elementary School.smrtsch");
            School testSchool1 = (School)serializer.Deserialize(temp.BaseStream);

            //WILL CHANGE BACK, ONLY TAKING PART OF THE SCHOOL FOR TESTING
            IEnumerable<SmartRoute.Data.Stop>  stops = testSchool1.Stops.Take(10);

            IEnumerable<SmartRoute.Data.Stop> activeStops =
                from stop in stops
                where (stop.Passengers > 0)
                select stop;


            List<SmartRoute.Data.Stop> activeStopsList = activeStops.ToList();

            IEnumerable<Location> activeLocations =
                from stop in stops
                where (stop.Passengers > 0)
                select (Location)stop;

            TimeSpan timeLimit = new TimeSpan(0, 0, 3600);

            //set up cached routing service
            DynamicRouteCache routeTableProvider = new DynamicRouteCache(routeServ);

            BusSolution solution = new BusSolution();
            bool[] results = new bool[30];
            double[] scores = new double[30];
            double[] thisScore = new double[30];

            for (int i = 0; i < 1; i++)
            {
                GenePool testPool = new GenePool(activeStopsList, activeStopsList.Count(), 10, false, routeTableProvider, 90, 90, 150, activeStops.ElementAt(0),
                    activeStops.ElementAt(1), 75, timeLimit, 90);

                double firstScore = 0;
                double secondScore = 0;
                for (int r = 0; r < 10; r++)
                {
                    
                    firstScore = testPool.BestScore;

                    for (int k = 0; k < 100; k++)
                    {
                        testPool.stepForwardGeneration();
                    }

                    secondScore = testPool.BestScore;
                    if ((secondScore - firstScore) <= 0)
                    {
                        //autostop
                        r = 10000;
                    }

                }
                testPool.testFitnessAll();
                thisScore[i] = testPool.BestScore;
                Chromosome champion = Chromosome.DeepCopy(testPool.Champion);
                double test = testPool.BestScore;
                solution = testPool.convertChromosomeToSolution(champion);
                double testCheck = testPool.CurrentTest.testFitness(solution);
                
                Chromosome best = new Chromosome(activeStopsList, activeStopsList.ElementAt(0), activeStopsList.ElementAt(1), testPool.Rand, true, 30);
                bool foundBetter = LocalSearch.neighborhoodSearch(1, champion, best, testPool.CurrentTest, testPool, ref scores[i]);
            }

            
            Imagery display = new Imagery(solution.Routes);
            this.Display = display;
             


        }

    }
}
